﻿@namespace BlazorFluentUI.Lists
@inherits FluentUIComponentBase
@typeparam TItem
@typeparam TKey
@using System.Reactive.Linq

@if (groupedUIListItems != null)
{
    <List TItem="IGroupedListItem3<TItem>"
             @ref="listReference"
             ClassName=@($"ms-GroupedList{(string.IsNullOrEmpty(ClassName) ? "" : " " + ClassName)}")
             IsVirtualizing=@IsVirtualizing
             ItemsSource=@groupedUIListItems
             StartIndex=0
             >
        <ItemTemplate>
            @if (context.Item is HeaderItem3<TItem,TKey>)
            {
                var header = context.Item as HeaderItem3<TItem,TKey>;
                bool isSelected = false;
                if (Selection != null && header != null)
                {
                    isSelected = Selection.IsRangeSelected(header.GroupIndex, header.Count);
                    if (!headerSubscriptions.ContainsKey(header))
                    {
                        var disposable = Selection.SelectionChanged.Subscribe(_ =>
                        {
                            Console.WriteLine("HeaderRangeCheck");
                            var selected = Selection.IsRangeSelected(header.GroupIndex, header.Count);
                            if (selected != isSelected)
                            {
                                isSelected = selected;
                                InvokeAsync(StateHasChanged);
                            }
                        });
                        headerSubscriptions.Add(header, disposable);
                    }

                }
                <GroupHeader IsOpen=@header!.IsOpen
                                OnOpenChanged=@((isOpen)=>
                                                {
                                                    if (header.IsOpen != isOpen)
                                                    {
                                                        header.IsOpen = isOpen;
                                                        InvokeAsync(StateHasChanged);
                                                    }
                                                })
                                Count=@header.Count
                                GroupLevel=@header.Depth
                                Name=@header.Name
                                Compact=@Compact
                                CurrentlySelected=@isSelected
                                IsSelectionCheckVisible=@(Selection != null)
                                OnClick=@(()=>OnHeaderClicked(context))
                                OnToggle=@(()=>OnHeaderToggled(context))
                                />
                @if (header.IsOpen)
                {
                    <GroupItemAuto ItemsSource=@header.Items
                                  TItem=@TItem
                                  TKey=@TKey
                                  GetKey=@(GetKey!)
                                  IsVirtualizing=@IsVirtualizing
                                  Compact=@Compact
                                  ItemTemplate=@(ItemTemplate!)
                                  SelectionMode=@SelectionMode
                                  Selection=@Selection
                                  StartIndex=@header.GroupIndex
                                  OnHeaderClick=@((a)=>OnHeaderClicked(a))
                                  OnHeaderToggle=@((a)=>OnHeaderToggled(a))
                                  />

                }
            }
            else
            {
                @if (context.Item!.IsVisible)
                {
                    @ItemTemplate!(context)
                }
            }
        </ItemTemplate>
    </List>

}